<template>
	<view class="invoice-container">
		<view class="form-container">
			<!-- 发票类型选择 -->
			<view class="form-item">
				<text class="label">发票类型</text>
				<view class="radio-group">
					<radio-group @change="onTypeChange" style="display: flex;align-items: center;">
						<label>
							<radio value="personal" :checked="invoiceType === 'personal'"
								style="transform: scale(0.9);" />
							个人发票
						</label>
						<label style="margin-left: 10px;">
							<radio value="company" :checked="invoiceType === 'company'"
								style="transform: scale(0.9);" />
							企业发票
						</label>
					</radio-group>
				</view>
			</view>

			<!-- 个人发票信息 -->
			<view>
				<view class="form-item">
					<view class="label">名称</view>
					<input type="text" placeholder="开票人姓名（必填）" v-model="name" class="input" />
				</view>

			</view>
			<!-- 企业发票信息 -->
			<view v-if="invoiceType === 'company'">
				<view class="form-item">
					<text class="label">税号</text>
					<input type="text" placeholder="纳税人识别号（必填）" v-model="taxnumber" class="input" />
				</view>
			</view>
			<view>
				<view class="form-item">
					<view class="label">手机</view>
					<input type="number" placeholder="开票人手机号" v-model="mobile" class="input" />
				</view>
				<view class="form-item">
					<view class="label">邮箱</view>
					<input type="text" placeholder="开票人邮箱" v-model="email" class="input" />
				</view>
			</view>
		</view>

		<!-- 企业发票信息 -->
		<view v-if="invoiceType === 'company'">
			<view style="padding: 10px;color: #999;">
				以下信息为非必填
			</view>
			<view style="background-color: #fff;padding: 15px;">
				<view class="form-item">
					<text class="label1">开户银行</text>
					<input type="text" placeholder="开户行名称" v-model="openbank" class="input" />
				</view>
				<view class="form-item">
					<text class="label1">银行账号</text>
					<input type="text" placeholder="开户银行账号" v-model="bankaccount" class="input" />
				</view>
				<view class="form-item">
					<text class="label1">单位电话</text>
					<input type="number" placeholder="单位联系电话" v-model="danweitelphone" class="input" />
				</view>
				<view class="form-item">
					<text class="label1">单位地址</text>
					<input type="text" placeholder="单位地址" v-model="danweiaddress" class="input" />
				</view>
			</view>
		</view>

		<!-- 保存按钮 -->
		<button class="save-btn" @click="saveInvoiceTitle">保存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				invoiceType: 'personal', // 默认个人发票
				personalName: '',
				companyName: '',
				taxNumber: '',
				address: '',
				phone: '',
				bankInfo: ''
			};
		},
		methods: {
			// 切换发票类型
			onTypeChange(e) {
				console.log(e)
				this.invoiceType = e.detail.value;
			},
			// 保存发票抬头
			saveInvoiceTitle() {
				// 表单验证
				if (this.invoiceType === 'personal') {
					if (!this.personalName.trim()) {
						uni.showToast({
							title: '请输入姓名',
							icon: 'none'
						});
						return;
					}
				} else {
					if (!this.companyName.trim()) {
						uni.showToast({
							title: '请输入公司名称',
							icon: 'none'
						});
						return;
					}
					if (!this.taxNumber.trim()) {
						uni.showToast({
							title: '请输入纳税人识别号',
							icon: 'none'
						});
						return;
					}
				}

				// 这里可以添加保存逻辑，比如调用API保存到服务器
				const invoiceData = {
					type: this.invoiceType,
					title: this.invoiceType === 'personal' ? this.personalName : this.companyName,
					taxNumber: this.taxNumber,
					address: this.address,
					phone: this.phone,
					bankInfo: this.bankInfo
				};

				// 模拟保存成功
				uni.showToast({
					title: '保存成功',
					icon: 'success'
				});

				// 保存成功后返回上一页
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			}
		}
	};
</script>

<style scoped>
	.form-container {
		background-color: #fff;
		padding: 20rpx;
	}

	.form-item {
		margin-bottom: 30rpx;
		display: flex;
		align-items: center;
	}

	.label {
		display: block;
		font-size: 28rpx;
		color: #666;
		width: 70px;
		text-align: center;
	}

	.label1 {
		display: block;
		font-size: 28rpx;
		color: #666;
		width: 80px;
		text-align: left;
	}

	.radio-group {
		display: flex;
		gap: 40rpx;
		padding: 10rpx 0;
	}

	.radio-label {
		display: flex;
		align-items: center;
		font-size: 28rpx;
		color: #333;
	}

	.input {
		width: 100%;
		height: 80rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 8rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		font-size: 28rpx;
	}

	.save-btn {
		height: 90rpx;
		line-height: 90rpx;
		background-color: #007aff;
		color: #fff;
		border-radius: 45rpx;
		font-size: 32rpx;
		margin-top: 40rpx;
	}
</style>